
//
// Copyright 2009 Arne Halvorsen (arne.halvorsen@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// Project page: http://code.google.com/p/mf4j/

//
// 091116 - aha42 - Starts in line change tracking.
// 091121 - aha42 - Added project page link (google code) to header comment.
//

package org.aha.mf4j.response;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * <p>
 *   XML content handler used to parse Flickr API XML response.
 * </p>
 * @author Arne Halvorsen (aha42)
 */
public final class FlickrXmlHandler extends DefaultHandler 
{
  private final StringBuilder m_sb=new StringBuilder();
  
  protected Locator m_loc=null;
  
  private FlickrObject m_root=null;
  
  private FlickrObject m_curr=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public FlickrXmlHandler(){}
  
  // Invoked prior to parse.
  void init()
  {
    m_sb.setLength(0);
    m_loc=null;
    m_root=null;
    m_curr=null;
  }
  
  @Override
  public final void characters(char[] str, int offset, int len) 
    throws SAXException 
  {
    m_sb.append(str, offset, len);
  }

  @Override
  public final void setDocumentLocator(Locator loc){ m_loc=loc; }
  
  @Override
  public void startElement(String uri, String lName, String name,
    Attributes attrs) throws SAXException 
  {
    String type=(lName==null || lName.length()==0) ? name : lName;
    
    FlickrObject curr=(m_curr==null) ? new FlickrObject(type) : 
                                       new FlickrObject(type, m_curr);
    
    if (m_root==null) m_root=curr;
    
    if (m_curr!=null) m_curr.addChild(curr);
      
    m_curr=curr;
      
    int n=attrs.getLength();
    for (int i=0; i<n; i++)
    {
      String aname=attrs.getQName(i);
      String value=attrs.getValue(i);
      value=(value==null) ? "" : value.trim();
      if (value.length()>0) curr.setAttribute(aname, value);
    }
  }
  
  @Override
  public void endElement(String uri, String lName, String name)
    throws SAXException 
  {
    String cdata=m_sb.toString();
    cdata=(cdata==null) ? "" : cdata;
    cdata=cdata.trim();
    if (cdata.length()>0) m_curr.setValue(cdata);
      
    m_curr=m_curr.getParent();

    m_sb.setLength(0);
  }
  
  /**
   * <p>
   *   Gets root of parsed hierarchy.
   * </p>
   * @return Parsed or {@code null} if none parsed.
   */
  public FlickrObject getRoot(){ return m_root; }
  
}
